import { Toolkit } from "../Framework/scripts/Toolkit";
import DragItem from "./DragItem";
import Editor from "./editor/Editor";
import GameAssetMgr from "./GameAssetMgr";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Drop extends cc.Component {


    @property(cc.Sprite)
    bg: cc.Sprite;

    @property(cc.Sprite)
    icon: cc.Sprite;

    private _interaction = true;
    get interaction() {
        return this._interaction;
    }
    set interaction(v) {
        if (v == this._interaction) return;
        this._interaction = v;
        this.bg.node.color = v ? cc.Color.WHITE : new cc.Color(213, 170, 170, 255);
    }

    start() {
        this.node.name = this.icon.spriteFrame && this.icon.spriteFrame.name || "";
        this.node.on(cc.Node.EventType.MOUSE_UP, (e) => {
            if (!this.interaction) return;
            //左键抬起
            if (e._button === 0) {
                if (DragItem.currentDragObj != null) {
                    this.icon.spriteFrame = DragItem.currentDragObj.icon.spriteFrame;
                    this.gid = this.icon.spriteFrame.name;
                    this.node.name = this.gid;
                    Toolkit.logTrace(`this.gid : ${this.gid}`);
                    DragItem.currentDragObj.reset();

                    if (!Editor.instance.count_table[this.gid]) {
                        Editor.instance.count_table[this.gid] = 0;
                    }
                    Editor.instance.count_table[this.gid]++;
                }
            }
        }, this);

        this.node.on(cc.Node.EventType.MOUSE_DOWN, (e) => {
            if (!this.interaction) return;
            //右键清除
            if (e._button === 2) {
                if (this.gid != "") {
                    this.clearIcon();
                }
            }
        }, this);

        //
        this.node.on(cc.Node.EventType.MOUSE_ENTER, (e) => {
            Editor.instance.currentSeletedObj = this;
        }, this);
        //
        this.node.on(cc.Node.EventType.MOUSE_LEAVE, (e) => {
            if (Editor.instance.currentSeletedObj === this) {
                Editor.instance.currentSeletedObj = null;
            }
        }, this);
    }

    gid = "";
    setIcon(gid) {
        if (gid != this.gid) {
            if (this.gid != "" && Editor.instance.count_table[this.gid]) {
                Editor.instance.count_table[this.gid]--;
            }

            this.gid = gid;
            this.node.name = this.gid;

            var spr = GameAssetMgr.instance.getSpriteByID(gid);
            this.icon.spriteFrame = spr;
            if (!Editor.instance.count_table[this.gid]) {
                Editor.instance.count_table[this.gid] = 0;
            }
            Editor.instance.count_table[this.gid]++;
        }
    }

    clearIcon() {
        if (Editor.instance.count_table[this.gid]) {
            Editor.instance.count_table[this.gid]--;
        }

        this.gid = "";
        this.node.name = "";
        this.icon.spriteFrame = null;
    }



}
